LoadingFrame.prototype = new Sprite();
LoadingBar.prototype = new Sprite ();

function LoadingBar (x,y,width,height,color) {
    Sprite.apply(this);
    this.width = width;
    this.height = height;
    this.x = x;
    this.y = y;
    this.color = color;
    
    this.init = function (){
        var g = this.graphics;
        //background
        g.beginFill(0xffffff);
        g.drawRect(0,0,this.width,this.height);
        g.endFill();
        this.update (0);
    }
    
    this.update = function (fillRatio) {
        var g = this.graphics;
        
        g.beginFill(this.color);
        g.drawRect(0,0,this.width*fillRatio,this.height);
        g.endFill();
        
        g.lineStyle (3,0);
        g.moveTo(0, 0);
        g.lineTo(this.width,0);
        g.lineTo(this.width,this.height);
        g.lineTo(0,this.height);
        g.lineTo(0,0);
    } 
}


function LoadingFrame (bgColor){
    Sprite.apply (this);
    this.width = MainContainerWidth;
    this.height = MainContainerHeight;
    this.x = 0;
    this.y = 0;
    this.bgColor = bgColor;
    this.loadingLabel = null;
    this.loadingBar = null;
    
    this.init = function (){
        var g = this.graphics;
        g.beginFill(this.bgColor);
        g.drawRect(0, 0, this.width, this.height);
        g.endFill();
        
        var barWidth = 500;
        var barHeight = 30;
        var barX = (MainContainerWidth - barWidth)/2;
        var barY = (MainContainerHeight - barHeight)/2;
        this.loadingBar = new LoadingBar(barX, barY, barWidth, barHeight, red);
        this.loadingBar.init();
        
        var tf = new TextFormat(font,25,0);
        
        this.loadingLabel = new TextField();
        this.loadingLabel.setTextFormat(tf);
        this.updateText (0);
        var labX = (MainContainerWidth - this.loadingLabel.width)/2;
        var labY = (MainContainerHeight - this.loadingLabel.height)/2 - barHeight - MARGIN;
        this.loadingLabel.x = labX;
        this.loadingLabel.y = labY;
        
        this.addChild (this.loadingBar);
        this.addChild(this.loadingLabel);
    }
    
    this.updateBar = function (fillRatio){
        this.loadingBar.update (fillRatio);
    }
    
    this.updateText = function (phase){
        switch (phase){
            case 0:
                this.loadingLabel.text ="Loading";
                break;
 
            case 1:
                this.loadingLabel.text ="Loading.";
                break;
            case 2:
                this.loadingLabel.text = "Loading..";
                break;
            case 3:
                this.loadingLabel.text = "Loading...";
                break;    
                
        }
        this.loadingLabel.width = this.loadingLabel.textWidth;
        this.loadingLabel.height = this.loadingLabel.textHeight;

    }
    
}